﻿using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using HDJ.Framework.Core;

public class SynchronizeFilesEditorWindow : EditorWindow
{
    [MenuItem("Tool/工具/文件同步(2000)", priority =2000)]
    static void DoIt()
    {
        //EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", "");
        SynchronizeFilesEditorWindow win= GetWindow<SynchronizeFilesEditorWindow>();
        win.Init();
    }

    List<SynchronizeFileInfo> infoList = new List<SynchronizeFileInfo>();
    private const string SynchronizeFileInfoName = "SynchronizeFileInfo";
    private void Init()
    {
       string json=  PlayerPrefs.GetString(SynchronizeFileInfoName);
        if (!string.IsNullOrEmpty(json))
        {
            infoList = JsonUtils.FromJson<List<SynchronizeFileInfo>>(json);
        }
    }

    private void OnEnable()
    {
        Init();
    }
    private void OnGUI()
    {

        GUILayout.Space(5);
        if (GUILayout.Button("+", GUILayout.Width(50)))
        {
            infoList.Add(new SynchronizeFileInfo());
        }
        GUILayout.Space(5);
        if (infoList == null)
            infoList = new List<SynchronizeFileInfo>();
        try
        {
            infoList = (List<SynchronizeFileInfo>)EditorDrawGUIUtil.DrawList("需要同步的路径：", infoList);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }

        //EditorDrawGUIUtil.DrawList("", test);
        GUILayout.Space(5);
        GUILayout.FlexibleSpace();

        if (GUILayout.Button("保存路径"))
        {
            string json = JsonUtils.ToJson(infoList);
            PlayerPrefs.SetString(SynchronizeFileInfoName, json);
            PlayerPrefs.Save();
        }
        GUILayout.Space(4);

        if (GUILayout.Button("同步"))
        {
            StartSynchronizeFile();
        }
    }

    private void StartSynchronizeFile()
    {
        foreach (var item in infoList)
        {
            string rawFilePath= item.rawFilePath.Replace("\\","/");
            string copyPath = item.copyPath.Replace("\\", "/");
            FileUtils.CopyDirectory(rawFilePath, copyPath);
        }
    }
}


public class SynchronizeFileInfo
{
    public string rawFilePath="";
    public string copyPath="";
}